
package cn.org.seventh.common;

import java.text.ParseException;
import java.util.Date;

import org.apache.commons.lang.time.DateUtils;

/**
 * @author <a href="mailto:ji.li@symbio-group.com">Andy</a>
 * @since Nov 30, 2007 4:17:56 PM
 */
public class Util {

    public static boolean isInt(String s) {
        boolean res = false;
        try {
            Integer.parseInt(s);
            res = true;
        } catch (Exception e) {
            res = false;
        }
        return res;
    }

    public static boolean isLong(String s) {
        boolean res = false;
        try {
            Long.valueOf(s);
            res = true;
        } catch (Exception e) {
        }
        return res;
    }

    public static boolean isDateFormat(String date) {
        boolean res = false;
        try {
            DateUtils.parseDate(date, new String[] { "yyyy/MM/dd",
                "yyyy.MM.dd", "yyyy-MM-dd" });
            res = true;
        } catch (Exception e) {
            res = false;
        }
        return res;
    }

    /**
     * @param sdate
     * @return if sucess, return date, or return null
     */
    public static Date toDate(String sdate) {
        if (isDateFormat(sdate)) {
            try {
                return DateUtils.parseDate(sdate, new String[] { "yyyy/MM/dd",
                    "yyyy.MM.dd", "yyyy-MM-dd" });
            } catch (ParseException e) {
            }
        }
        return null;
    }
}
